<?php
/**
 * Created by PhpStorm.
 * User: tsy
 * Date: 2017/6/7
 * Time: 15:43
 */
namespace Admin\Controller\Common;
use Think\Controller;

class VPNController extends Controller{
    private $login_url="https://vpn.shanghai-cis.com.cn/+CSCOE+/logon.html";
    private $index_url="https://vpn.shanghai-cis.com.cn/+webvpn+/index.html";
    private $batch_url="https://vpn.shanghai-cis.com.cn/+CSCO+00756767633A2F2F6A6A6A2E617370662E70627A++/webservice/batchcredit?wsdl";

    private $username="hnflhsadmin";
    private $password="5crcnxh2";
    private $orgcode="Q10155500H2K00";
    private $ssecret="trzj4g2g";
    protected $ch="";

    public function __construct(){
        $this->ch=curl_init();
    }
    function VPN_login(){
        $cookie_path = dirname(__FILE__).'/cookie/';
        $ch=$this->ch;

        $params[CURLOPT_URL] = $this->login_url;    //请求url地址
        $params[CURLOPT_HEADER] = true; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';



        //判断是否有cookie,有的话直接使用
        if (isset($_COOKIE['cookie_jar']) && ($_COOKIE['cookie_jar'] || is_file($_COOKIE['cookie_jar'])))
        {
            $params[CURLOPT_COOKIEFILE] = $_COOKIE['cookie_jar']; //这里判断cookie
        }
        else
        {
            $cookie_jar = tempnam($cookie_path, 'cookie'); //产生一个cookie文件
            $params[CURLOPT_COOKIEJAR] = $cookie_jar; //写入cookie信息
            setcookie('cookie_jar', $cookie_jar); //保存cookie路径
        }
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_SSL_CIPHER_LIST]='TLSv1';
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        if($error=curl_error($ch)){
            die($error);
        }

        $url1=$this->index_url;
        $vars['tgroup'] ='';
        $vars['next']='';
        $vars['tgcookieset']='';
        $vars['Login'] = '登录';
        $vars['username'] = $this->username;
        $vars['password'] = $this->password;
//          $ch = curl_init();
        $params[CURLOPT_URL] = $url1;    //请求url地址
        $params[CURLOPT_HEADER] = true; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_USERAGENT] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
        $postfields = '';
        foreach ($vars as $key => $value){
            $postfields .= urlencode($key) . '=' . urlencode($value) . '&';
        }
        $postfields=substr($postfields, 0,strlen($postfields)-1);
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $postfields;
        //判断是否有cookie,有的话直接使用
        if (isset($_COOKIE['cookie_jar']) && ($_COOKIE['cookie_jar'] || is_file($_COOKIE['cookie_jar'])))
        {
            $params[CURLOPT_COOKIEFILE] = $_COOKIE['cookie_jar']; //这里判断cookie
        }
        else
        {
            $cookie_jar = tempnam($cookie_path, 'cookie'); //产生一个cookie文件
            $params[CURLOPT_COOKIEJAR] = $cookie_jar; //写入cookie信息
            setcookie('cookie_jar', $cookie_jar); //保存cookie路径
        }
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行

        return $content;
    }

    function queryCredit($identity,$custname){
        $ch=$this->ch;
        $cookie_path = dirname(__FILE__).'/cookie/';
        header ( 'Content-type:text/html;charset=UTF-8' );
        $url2=$this->batch_url;

        $xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.creditreport.p2p.sino.com/\">
			<soapenv:Header/>
			<soapenv:Body>
			<web:queryCredit>
			<orgcode>". $this->orgcode ."</orgcode>
			<secret>". $this->ssecret ."</secret>
			<plate>1</plate>
			<certtype>0</certtype>
			<certno>".$identity."</certno>
			<name>".$custname."</name>
			<reason>02</reason>
			<createtype>1</createtype>
			</web:queryCredit>
			</soapenv:Body>
			</soapenv:Envelope>";

//         $ch = curl_init();
        $params[CURLOPT_URL] = $url2;    //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_USERAGENT] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
//         $postfields = '';
//         foreach ($vars as $key => $value){
//         	$postfields .= urlencode($key) . '=' . urlencode($value) . '&';
//         }
//         $postfields=substr($postfields, 0,strlen($postfields)-1);
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $xml;
        //判断是否有cookie,有的话直接使用
        if (isset($_COOKIE['cookie_jar']) && ($_COOKIE['cookie_jar'] || is_file($_COOKIE['cookie_jar'])))
        {
            $params[CURLOPT_COOKIEFILE] = $_COOKIE['cookie_jar']; //这里判断cookie
        }
        else
        {
            $cookie_jar = tempnam($cookie_path, 'cookie'); //产生一个cookie文件
            $params[CURLOPT_COOKIEJAR] = $cookie_jar; //写入cookie信息
            setcookie('cookie_jar', $cookie_jar); //保存cookie路径
        }
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }
    //解析XML
    function getArray($node) {
        $array = false;

        if ($node->hasAttributes()) {
            foreach ($node->attributes as $attr) {
                $array[strtolower($attr->nodeName)] = $attr->nodeValue;
            }
        }

        if ($node->hasChildNodes()) {
            if ($node->childNodes->length == 1) {
                $array[strtolower($node->firstChild->nodeName)] = $this->getArray($node->firstChild);
            } else {
                foreach ($node->childNodes as $childNode) {
                    if ($childNode->nodeType != XML_TEXT_NODE) {
                        $array[strtolower($childNode->nodeName)][] = $this->getArray($childNode);
                    }
                }
            }
        } else {
            return $node->nodeValue;
        }
        return $array;
    }

}